#!/usr/bin/env python
# encoding: utf-8


"""
@file: fangchengyoujie.py
@time: 2017/5/3 上午10:57
"""
# 方程有解问题
from mathsolver.functions.base import *
from mathsolver.functions.sanjiao import sanjiao_utils as su
from mathsolver.functions.hanshu.dingyiyu import QiuDingYiYu


# 关于x的三角方程有解问题 元函数定义 三角函数-题型->39方程有解问题
# Style1 Input:方程(含有两个参数的方程) Ouput 参数的值域
class SanJiaoFangChengYouJie(BaseFunction):
    @staticmethod
    def _solve_param_value(f, param_symb):
        return su.solve_trig_eq(BaseEq([f, '0']), param_symb)[0]

    def solver(self, *args):
        self.label.add('方程有解的定义')
        left, right = args[0].sympify()
        _x = sympify('x')
        f = left - right
        _parm = list(set(f.free_symbols) - {_x})[0]
        param_expr = SanJiaoFangChengYouJie._solve_param_value(f, _parm)
        n_eq = BaseEq([param_expr, _parm])
        g1, g2 = n_eq.sympify()  # g1为原三角函数, g2为关于参数的函数
        self.steps.append(['有题意可知:', BaseEq([g1, g2]).printing()])
        self.steps.append(
            ['令' + BaseEq(['g(%s)' % str(_x), g1]).printing(), ' ; ' + BaseEq(['g(%s)' % str(_parm), g2]).printing()])

        _dingyiyu = QiuDingYiYu().solver(BaseEq(['y', g1]), self.search(_x))
        # _intl = self.search(_x).intersection(_dingyiyu)
        from mathsolver.functions.sanjiao.zuizhi import SanJiaoHanShuZuiZhi
        g1_range = SanJiaoHanShuZuiZhi(known={_x: _dingyiyu}).solver(BasePoly(g1), '取值范围').output[
            0].interval  # 求出g1的值域
        self.steps.append(['求得' + new_latex(sympify('g(%s)' % str(_x))), ' \\in ' + new_latex(g1_range)])
        from mathsolver.functions.hanshu.helper import yizhizhiyu_qiufanwei
        param_interval = yizhizhiyu_qiufanwei(g2, g1_range)
        self.steps.append(['求得' + new_latex(_parm), '取值范围为:' + new_latex(param_interval)])
        return self


if __name__ == '__main__':
    pass
